package com.company.process.ds;

import com.company.TrianglePacket;
import com.company.graph.Graph;
import com.company.graph.NopowerNoDirectEdgeWithNodeGraph;
import com.company.graph.PowerEdge;
import com.company.graphgetter.EdgeGetterWithGraph;
import com.company.graphgetter.GraphGetter;

import java.util.*;

public class GraphDS2Process extends GraphDSProcess {

    public GraphDS2Process(int theta) {
        super(theta);
    }

    @Override
    public void process(Graph graph, GraphGetter getter) {
        G = (NopowerNoDirectEdgeWithNodeGraph) graph;
        this.getter = (EdgeGetterWithGraph) getter;
        //Group all triangular edges with their ET values into "e_plus" and "e_minus" respectively
        List<TrianglePacket> edgeRanks = this.getter.edgeRanks();
        for (TrianglePacket rank : edgeRanks) {
            setET(rank.getId(), rank.getValue());
            if (rank.getValue() >= theta) {
                e_plus.add(G.getEdgeById(rank.getId()));
            } else {
                e_minus.add(G.getEdgeById(rank.getId()));
            }
        }
        for (PowerEdge alpha : e_plus) {
            int alphaId = alpha.getId();
            Integer ret = getRET(alphaId);
            if (ret > theta) {
//                System.out.println("DEBUG alphaId:" + alphaId);
                //available to delete an edge
                if (G.getEdgeById(alphaId) == null) {
                    continue;
                }
                List<PowerEdge> relatedEdges = G.getRelatedEdges(alphaId);
//                System.out.println("DEBUG relatedEdges:" + relatedEdges);
                //删除掉e_plus集合中的边
//                relatedEdges.removeIf(next -> e_plus.contains(next));
//                System.out.println("DEBUG after remove relatedEdges:" + relatedEdges);
                ArrayList<Bag> bags = Bag.generateFromEdges(relatedEdges, ET);
                Collections.sort(bags);
                //需要被删除的次数
                int deleteCount = ret - theta;
                deleteRelatedEdgesWithBags(bags, deleteCount);
            }
        }

    }

}
